###
class Ball
  constructor: (@color, @point) -> #
class Point
  constructor: (@x, @y) -> #
  bounce: -> puts "hello"
multer = (m) -> ((x) -> x*m)
tripler = multer(3)
alert tripler(10)
###

$ ->
  $(".idea").draggable
    drag: (event, ui) -> 
      $.notification "(#{ui.position.top}, #{ui.position.left})",
        position: "bottom-right"
  # $(".note").resizable()
    
  $("#dialog").dialog() # "option", "title", 'Dialog Title'

  $.ajax
    url: "batman"
    type: "POST"
    data:
      text: "Hello you"
    success: (data) ->
      $.notification data

  $.ajax
    url: "ideasdata"
    dataType: "json"
    # cache: yes
    timeout: 5000 #ms
    success: (data) ->
      # alert "data received!"
      $("#dialog").append """
        <h3>Data received</h3>
        <p>#{data.notes}</p>
      """
      _.each data.notes, (caption) ->
        $("#ideatable").append("<div class='idea ui-widget-content'>"+caption+"</div>")
        $(".idea").dblclick -> alert "you clickd me"
        $(".idea").draggable()
    error: (jqXHR, textStatus, errorThrown) ->
      # alert "error: #{textStatus} - #{errorThrown}"

      $("#dialog").append """
        <h3>#{textStatus}</h3>
        <p>#{errorThrown}</p>
      """

      # $.notification """
      # """,
      #   duration: 4000
      #   position: "bottom-right"
      ###
      $.notification textStatus,
        className: "jquery-notification"
        duration: 2000
        freezeOnHover: true
        hideSpeed: 250
        position: 'center'
        showSpeed: 250
        zIndex: 99999
      ###


  sizeScrollbar = ->
    remainder = scrollContent.width() - scrollPane.width()
    proportion = remainder / scrollContent.width()
    handleSize = scrollPane.width() - (proportion * scrollPane.width())
    scrollbar.find(".ui-slider-handle").css
      width: handleSize
      "margin-left": -handleSize / 2

    handleHelper.width("").width scrollbar.width() - handleSize
  resetValue = ->
    remainder = scrollPane.width() - scrollContent.width()
    leftVal = (if scrollContent.css("margin-left") is "auto" then 0 else parseInt(scrollContent.css("margin-left")))
    percentage = Math.round(leftVal / remainder * 100)
    scrollbar.slider "value", percentage
  reflowContent = ->
    showing = scrollContent.width() + parseInt(scrollContent.css("margin-left"), 10)
    gap = scrollPane.width() - showing
    scrollContent.css "margin-left", parseInt(scrollContent.css("margin-left"), 10) + gap  if gap > 0
  scrollPane = $(".scroll-pane")
  scrollContent = $(".scroll-content")
  scrollbar = $(".scroll-bar").slider(slide: (event, ui) ->
    if scrollContent.width() > scrollPane.width()
      scrollContent.css "margin-left", Math.round(ui.value / 100 * (scrollPane.width() - scrollContent.width())) + "px"
    else
      scrollContent.css "margin-left", 0
  )
  handleHelper = scrollbar.find(".ui-slider-handle").mousedown(->
    scrollbar.width handleHelper.width()
  ).mouseup(->
    scrollbar.width "100%"
  ).append("<span class='ui-icon ui-icon-grip-dotted-vertical'></span>").wrap("<div class='ui-handle-helper-parent'></div>").parent()
  scrollPane.css "overflow", "hidden"
  $(window).resize ->
    resetValue()
    sizeScrollbar()
    reflowContent()

  setTimeout sizeScrollbar, 10